
/**
 * 处理下拉刷新的数据
 * @param {*} actionType 
 * @param {*} dispatch 
 * @param {*} storeName 
 * @param {*} data 
 * @param {*} pageSize 
 * @param {*} favoriteDao 
 */

import ProjectModel from "../model/ProjectModel"
import Utils from "../util/Utils"


export function handleData(actionType, dispatch, storeName, data, pageSize, favoriteDao, params) {
    let fixItems = [];
    if (data && data.data) {
        if (Array.isArray(data.data)) {
            fixItems = data.data;
        } else if (Array.isArray(data.data.items)) {
            fixItems = data.data.items;
        }
    } else {
        fixItems = data;
    }
    //第一次要加载的数据
    let showItems = pageSize > fixItems.length ? fixItems : fixItems.slice(0, pageSize);
    _projectModels(showItems, favoriteDao, projectModels => {
        dispatch({
            type: actionType,
            items: fixItems,
            projectModels: projectModels,
            storeName,
            pageIndex: 1,
            ...params
        })
    });
}

export async function _projectModels(showItems, favoriteDao, callBack) {
    let keys = []
    try {
        keys = await favoriteDao.getFavoriteKeys();
    } catch (error) {
        console.log(error)
    }
    let projectModels = []
    for (let i = 0, len = showItems.length; i < len; i++) {
        projectModels.push(new ProjectModel(showItems[i], Utils.checkFavorite(showItems[i], keys)))
    }

    if (typeof callBack === 'function') {
        callBack(projectModels)
    }
}

export const doCallBack = (callBack, object) => {
    if (typeof callBack === 'function') {
        callBack(object);
    }
};